import fs from 'fs'

/**
 * 检查文件夹是否存在
 * @param _folder
 * @returns {Promise<any>}
 */
function checkFolder(_folder) {
  return new Promise((resolve, reject) => {
    fs.access(_folder, (err) => {
      err ? resolve(false) : resolve(true)
    })
  })
}

function createFolder(_folder) {
  return new Promise((resolve, reject) => {
    fs.mkdir(_folder, (err) => {
      err ? resolve(false) : resolve(true)
    })
  })
}

function dateFormat(second) {
  let dd, hh, mm, ss
  second = typeof second === 'string' ? parseInt(second) : second
  if (second < 0) {
    return
  }
  // 天
  dd = second / (24 * 3600) | 0
  second = Math.round(second) - dd * 24 * 3600
  // 小时
  hh = second / 3600 | 0
  second = Math.round(second) - hh * 3600
  // 分
  mm = second / 60 | 0
  // 秒
  ss = Math.round(second) - mm * 60
  if (Math.round(dd) < 10) {
    dd = dd > 0 ? dd : ''
  }
  // if (Math.round(hh) < 10) {
  //   hh = '0' + hh
  // }
  if (Math.round(mm) < 10) {
    mm = '0' + mm
  }
  if (Math.round(ss) < 10) {
    ss = '0' + ss
  }
  return {
    dd,
    hh,
    mm,
    ss
  }
}

/**
 * 倒计时
 * @param mtKey
 * @param callBack
 */
function countdown(mtKey, callBack) {
  mtKey = setInterval(() => {
    callBack(mtKey)
  }, 1000)
}

export {
  checkFolder,
  createFolder,
  dateFormat,
  countdown
}
